package models

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/gorm"
	"mz_water/entity"
	"mz_water/util"
	"time"
)

//分页信息

// user 模型
type User struct {
	Id         string    `gorm:"column:Id";json:"id"`
	Name       string    `gorm:"column:Name";json:"name"`
	Pwd        string    `gorm:"column:Pwd";json:"_"`
	Phone      string    `gorm:"column:Phone";json:"phone"`
	Money      float64   `gorm:"column:Money";json:"money"`
	Device     string    `gorm:"column:Device";json:"device"`
	Sex        int       `gorm:"column:Sex";json:"phone"`
	Start      int       `gorm:"column:Start";json:"start"`
	Type       int       `gorm:"column:Type";json:"type"`
	CreateTime time.Time `gorm:"column:CreateTime";json:"createTime"`
	UpTime     time.Time `gorm:"column:UpTime";json:"upTime"`
}

func (u User) TableName() string {
	return "mz_user"
}

// 根据ID获取user
func GetUser(id string) (res *entity.Result) {
	res = &entity.Result{}
	qur := &User{Id: id}
	err := util.GetDb().First(qur).Error
	if gorm.IsRecordNotFoundError(err) {
		res.SetCode(200)
		res.SetMessage("查询不到数据")
		res.SetData(err)
		return res
	} else if err != nil {
		//如果err不等于record not found错误，又不等于nil，那说明sql执行失败了。
		fmt.Println("查询失败", err)
		res.SetCode(201)
		res.SetMessage("查询失败")
		res.SetData(qur)
		return res
	}
	res.SetCode(200)
	res.SetMessage("查询成功")
	res.SetData(qur)
	return res
}

// 根据全部user
func GetUserList(c *gin.Context) (res *entity.Result) {
	res = &entity.Result{}
	mz_user := []User{}
	//page, _ := strconv.Atoi(c.Query("page"))
	//pageSize, _ := strconv.Atoi(c.Query("page_size"))
	util.GetDb().Find(&mz_user)
	//if id, isExist := c.GetQuery("id"); isExist == true {
	//	data.Where("Id=?", id)
	//}
	//if name, isExist := c.GetQuery("name"); isExist == true {
	//	data.Where("Name=?", name)
	//}
	//if phone, isExist := c.GetQuery("phone"); isExist == true {
	//	data.Where("Phone=?", phone)
	//}
	//if page > 0 && pageSize > 0 {
	//	data.Limit(pageSize).Offset((page - 1) * pageSize)
	//}
	type UserList struct {
		UserList interface{}
	}
	userList := &UserList{}
	userList.UserList = mz_user
	res.SetData(userList)
	res.SetMessage("查询成功")
	return res
}

func (u *User) DeleteUser(id string) {

}
